#This makefile is included in all the other makefiles

DEFAULT_GOAL := all

#what to name the intermediate .elf and output .bin file
BINNAME = openwii

COMMONCFILES = ../common/serial.o ../common/main.o
COMMONHEADERS = ../common/serial.h ../common/common.h
LOADERFILES = ../common/bootloader.o

#If your compilers are called something different change these...
LD = $(CROSS_COMPILE_PATH)$(CROSS_COMPILE_PREFIX)ld
CC = $(CROSS_COMPILE_PATH)$(CROSS_COMPILE_PREFIX)gcc
AS = $(CROSS_COMPILE_PATH)$(CROSS_COMPILE_PREFIX)as
CP = $(CROSS_COMPILE_PATH)$(CROSS_COMPILE_PREFIX)objcopy

CFLAGS += -D$(chip)
CFLAGS +=-DBINARYSTARTDMS=_binary_mn10200_dms_bin_start -DBINARYSIZEDMS=_binary_mn10200_dms_bin_size
CFLAGS +=-DBINARYSTARTD2B=_binary_mn10200_d2b_bin_start -DBINARYSIZED2B=_binary_mn10200_d2b_bin_size

#CFILES requires these prerequisites
#$(CFILES):  $(HEADERS)

# This defines how to handle the various file types in/out
.S.o:
	$(AS) $(AFLAGS) $< -o $*.o
.c.o:
	$(CC) $(CFLAGS) $< -o $*.o

#if and only if they specified a ldscript then add that to the linker script
ifdef LDSCRIPT
LDFLAGS += -T$(LDSCRIPT)
endif

#link rules and prerequisites
ifdef withbootloader
BINNAME = openwii-loader
%.elf: $(ASMFILES) $(CFILES) $(COMMONCFILES) $(LOADERFILES)
	$(LD) $(LDFLAGS) $^ -o $@ -b binary $(BINARYS)
%.hex: $(BINNAME).elf
	$(CP) -I elf32-avr -O ihex $(BINNAME).elf $(BINNAME).hex
%.bin: $(BINNAME).elf
	$(CP) -I elf32-avr -O binary $(BINNAME).elf $(BINNAME).bin
%.h:
else
%.elf: $(ASMFILES) $(CFILES) $(COMMONCFILES)
	$(LD) $(LDFLAGS) $^ -o $@ -b binary $(BINARYS)
%.hex: $(BINNAME).elf
	$(LD) --oformat ihex $< -o $@
%.bin: $(BINNAME).elf
	$(LD) --oformat binary $< -o $@
%.h:
endif

#These sections below define the different make types
# for instance make sys will only make the system files, make ram only makes the
# ram files, make clean will erase all compiled files

# First rule is allways default! (unless u define a default)
# make everythng
all : link

#Stuff thats coded in c
c: $(CFILES) $(COMMONCFILES) $(LOADERFILES)

#stuff thats coded in asm
asm: $(ASMFILES)

# ofc a make clean, very handy
clean:
	rm -f *.o *.elf *.bin *.hex ../common/*.o
